<?php
class LocationsController extends AppController {
	var $name='Locations';
	var $uses=array('Province','City');
	var $components = array('Session');
	
	function index()	
	{
            if (isset ($this->current_user)) {
                if ($this->current_user['User']['city_id']) {
                    $users = $this->paginate('User',array('User.city_id'=>$this->current_user['User']['city_id']));
                }else if ($this->current_user['User']['province_id']) {

                    $cities = $this->City->find('all',array('conditions'=>array('City.province_id'=>$this->current_user['User']['province_id'])));
                }
            }
		if(isset($this->params['city'])){
			$city = $this->City->findByCity($this->params['city']);
			if(!is_null($city)){
				$this->User->id = $this->current_user_id;
				$this->User->saveField('city_id',$city['City']['id']);
				$this->redirect('/settings');
			}
		}
		if(isset($this->params['province'])){
			echo $this->params['province'];
			$province = $this->Province->findByProvince($this->params['province']);
			$cities = $this->City->findAllByFatherid($province['Province']['provinceid']);
			$this->set('province',$province);
			$this->set('cities',$cities);
		}else{
			$provinces = $this->Province->find('all');
			$this->set('provinces',$provinces);
		}
	}

        function provinces($name=null) {
            if ($name) {
				$province = $this->Province->findByName($name);
				if ($province) {
					$p_id = $province['Province']['id'];
				}
                $this->set('cities', $this->City->find('all',array('conditions'=>array('City.province_id'=>$p_id))));
				$this->render();
            }
            $this->set('provinces', $this->Province->find('all'));
        }
	
}

?>